//
//  RVReciteWordsViewController.m
//  RememberVocabularies
//
//  Created by 伍银荣 on 2017/10/30.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "RVReciteWordsViewController.h"
#import "RVWordDetailView.h"
#import "SVProgressHUD.h"

@interface RVReciteWordsViewController ()<UIScrollViewDelegate,RVWordDetailViewDelegate>

@property (nonatomic, strong) UIScrollView *contentView;

@property (nonatomic, assign) NSInteger count;
@property(nonatomic, assign) BOOL isLast;
@end


@implementation RVReciteWordsViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
     _isLast = false;
    
    [self setUpUI];
}
-(void) setUpUI
{
    self.navigationItem.title = @"开始背单词吧";
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *value = [userDefaults valueForKey:@"nowPlan"];
    self.count = [value[@"wordNum"] integerValue];
    
    self.contentView.frame = CGRectMake(0, 0, Width, Height);
    for (int i = 0; i < self.count; i++) {
        RVWordDetailView *view = [[RVWordDetailView alloc]init];
        //传到每一个view的内容
        view.wordsModel = self.wordsArr[i];
        view.delegate = self;
        view.frame = CGRectMake(i * Width, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
        // view.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:view];
    }
    self.contentView.contentSize = CGSizeMake(Width * self.count, Height - 64);
    [self.view addSubview:self.contentView];
}
//点击收藏按钮
-(void)collectedWord:(NSString *)word
{
    [self saveData:collectedPlist word:word msg:@"收藏"];
}
//点击已背按钮
-(void)finishedWord:(NSString *)word
{
    [self saveData:finishPlist word:word msg:@"记录"];
}

-(void) saveData:(NSString *)plistPath word:(NSString *)word msg:(NSString *)msg
{
    NSString *str = [[NSUserDefaults standardUserDefaults] valueForKey:@"nowPlan"][@"bookPath"];
    //拿到plist文件路径的数据
    NSDictionary *Dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    //创建一个可变字典将拿到数据的Dict赋值到可变字典
    NSMutableDictionary *mDict = [[NSMutableDictionary alloc]initWithDictionary:Dict];
    //创建一个对应书的单词数组，用来存储单词
    NSMutableArray *bookMarr;
    for (NSString *string in [mDict allKeys]) {
        if ([string isEqualToString:str]) {
            bookMarr = [NSMutableArray arrayWithArray:[mDict valueForKey:str]];
        }
    }
    //判断bookMarr的数据是否为空，如果为空直接添加到可变数组中
    if (bookMarr.count == 0) {
        [bookMarr addObject:word];
    } else {
        //如果不为空，定义一个bool变量，判断bookMarr中为是否有相同的单词，初始值为false,如果有相同的单词则为true
        BOOL isSame = false;
        //forin 遍历判断点击当前页面的单词与bookMarr中是否相同
        for (NSString *str in bookMarr) {
            if ([word isEqualToString:str]) {
                //相同，为true
                isSame = true;
            }
        }
        //forin 遍历完成，判断isSame的值
        if (isSame) {
            //如果为true， 直接提示已经记录，并且return
            [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"您已经%@了！",msg]];
            return;
        } else {
            //如果为false，则说明没有相同的，将word添加到可变数组中
            [bookMarr addObject:word];
        }
    }
    //将存单词的数组赋值给可变字典键对应的值中
    mDict[str] = bookMarr;
    BOOL isSuccess = [mDict writeToFile:plistPath atomically:YES];
    if (isSuccess) {
        [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@成功",msg]];
    } else {
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@失败",msg]];
    }
}

- (UIScrollView *)contentView
{
    if(_contentView == nil)
    {
        UIScrollView *scrollView = [[UIScrollView alloc]init];
        scrollView.backgroundColor = [UIColor whiteColor];
        scrollView.frame = [UIScreen mainScreen].bounds;
        scrollView.bounces = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        [scrollView setMaximumZoomScale:2.0f];
        [scrollView setMinimumZoomScale:0.2f];
        scrollView.delegate = self;
        scrollView.contentSize = CGSizeMake(Width * self.count, Height - 64);
        _contentView = scrollView;
    }
    return _contentView;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger currentPage = scrollView.contentOffset.x/self.view.bounds.size.width + 1;
    if (currentPage == self.count) {
        if (_isLast) {
            return;
        }
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSInteger sum = [userDefaults integerForKey:@"sum"];
        NSLog(@"sum;;;;;->%ld",sum);
        sum += self.count;
        NSLog(@"sum;;;;;->%ld",sum);
        [userDefaults setObject:@(sum) forKey:@"sum"];
        _isLast = true;
    }
}
@end
